/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.attach;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.ui.tree.DropTree;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachCatalog;
import com.unis.service.attach.AttachCatalogService;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2013-03-05
 */
@Controller
@RequestMapping("/attachcatalog")
public class AttachCatalogController {

    private AttachCatalogService attachcatalogService;

    /****************************************
     * Insert a instace of AttachCatalog
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertAttachCatalog(@RequestBody AttachCatalog attachcatalog) throws Exception {
        attachcatalog.setStatmentId("AttachCatalog.insert");
        String status = "failure";
        attachcatalogService.insert(attachcatalog);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of AttachCatalog
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody AttachCatalog attachcatalog) throws Exception {
        attachcatalog.setStatmentId("AttachCatalog.update");
        String status = "failure";
        attachcatalogService.update(attachcatalog);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of AttachCatalog
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody AttachCatalog attachcatalog) throws Exception {
        attachcatalog.setStatmentId("AttachCatalog.delete");
        String status = "failure";
        attachcatalogService.batchDelete(attachcatalog);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of AttachCatalog
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        AttachCatalog attachcatalog = new AttachCatalog();
        attachcatalog.setStatmentId("AttachCatalog.queryAsList");
        BeanUtils.populate(attachcatalog, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(attachcatalog, pageMap);
        List<BaseModel> attachcatalogList = attachcatalogService.queryAsList(attachcatalog);
        Map<String, Object> renderMap = GridUIMapper.render(attachcatalog, attachcatalogList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of AttachCatalog
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody AttachCatalog attachcatalog) throws Exception {
        attachcatalog.setStatmentId("AttachCatalog.queryAsObject");
        return attachcatalogService.queryAsObject(attachcatalog);
    }

    @RequestMapping("/zTree")
    @ResponseBody
    public Object queryAsTree(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        AttachCatalog catalog = new AttachCatalog();
        catalog.setStatmentId("AttachCatalog.queryAsList");
        catalog.setPaging(false);
        BeanUtils.populate(catalog, paramMap);
        List<BaseModel> catalogList = attachcatalogService.queryAsList(catalog);
        if (!paramMap.containsKey("catalogId")) {
            AttachCatalog rootCatalog = new AttachCatalog();
            rootCatalog.setCatalogId("0");
            rootCatalog.setCatalogName("附件类别");
            rootCatalog.setPid(null);
            rootCatalog.setIsParent("true");
            rootCatalog.setIsShare("true");
            catalogList.add(rootCatalog);
        }
        return catalogList;
    }

    @RequestMapping("/page")
    public String forword(DropTree dropTree, ModelMap map) {
        map.put("dropTree", dropTree);
        return "attach/attachcatalogList";
    }

    @Resource
    public void setAttachCatalogService(AttachCatalogService attachcatalogService) {
        this.attachcatalogService = attachcatalogService;
    }
}
